package com.mdd.admin.controller.course;

import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.course.ICourseService;
import com.mdd.admin.validate.course.CourseColumnParam;
import com.mdd.admin.validate.course.CourseParam;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.course.CourseColumnListVo;
import com.mdd.admin.vo.course.CourseListVo;
import com.mdd.admin.vo.course.CourseDetailVo;
import com.mdd.admin.vo.course.UserCourseListVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.validator.annotation.IDMust;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 课程管理
 */
@RestController
@RequestMapping("api/course")
public class CourseController {

    @Resource
    ICourseService iCourseService;

    /**
     * 课程列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<CourseListVo> list = iCourseService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 用户的课程列表
     * @return Object
     */
    @GetMapping("/listByUser")
    public Object listByUser(@RequestParam("userId") Integer userId) {
        return AjaxResult.success(iCourseService.listByUser(userId));
    }

    /**
     * 专栏课程关联列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/column/list")
    public Object listColumns(@Validated PageParam pageParam,
                       @RequestParam(value="courseId", required = true) Integer courseId,
                       @RequestParam Map<String, String> params) {
        Assert.notNull(courseId, "courseId参数缺失");
        List<CourseColumnListVo> list = iCourseService.listColumns(pageParam, params, courseId);
        return AjaxResult.success(list);
    }


    /**
     * 课程详情
     *
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/detail")
    public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        CourseDetailVo detail = iCourseService.detail(id);
        return AjaxResult.success(detail);
    }

    /**
     * 课程新增
     *
     * @param courseParam 参数
     * @return Object
     */
    @Log(title = "课程新增")
    @PostMapping("/add")
    public Object add(@Validated(value = CourseParam.create.class) @RequestBody CourseParam courseParam) {
        iCourseService.add(courseParam);
        return AjaxResult.success(true);
    }

    /**
     * 课程编辑
     *
     * @param courseParam 参数
     * @return Object
     */
    @Log(title = "课程编辑")
    @PostMapping("/edit")
    public Object edit(@Validated(value = CourseParam.update.class) @RequestBody CourseParam courseParam) {
        iCourseService.edit(courseParam);
        return AjaxResult.success(true);
    }

    /**
     * 课程删除
     *
     * @param param 参数
     * @return Object
     */
    @Log(title = "课程删除")
    @PostMapping("/del")
    public Object del(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        Assert.isTrue(StringUtil.isNotEmpty(id), "id不能为null");
        iCourseService.del(id);
        return AjaxResult.success(true);
    }


    /**
     * 课程状态
     *
     * @author panweiliang
     * @param param 课程参数
     * @return AjaxResult
     */
    @Log(title = "课程上下架状态")
    @PostMapping("/change")
    public AjaxResult change(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        String status = param.get("status");
        Assert.isTrue(StringUtil.isNotEmpty(id), "id不能为null");

        Assert.isTrue(status == null || "".equals(status) || "1".equals(status) || "0".equals(status), "status不在取值范围内");
        iCourseService.changeStatus(id, status);
        return AjaxResult.success(true);
    }


    /**
     * 专栏课程关联新增
     *
     * @author panweiliang
     * @param courseColumnParam 参数
     * @return Object
     */
    @Log(title = "专栏课程关联新增")
    @PostMapping("/column/add")
    public Object addColumn(@Validated(value = CourseColumnParam.create.class) @RequestBody CourseColumnParam courseColumnParam) {
        iCourseService.addColumn(courseColumnParam);
        return AjaxResult.success();
    }

    /**
     * 专栏课程关联删除
     *
     * @author panweiliang
     * @param courseColumnParam 参数
     * @return Object
     */
    @Log(title = "专栏课程关联删除")
    @PostMapping("/column/del")
    public Object delColumn(@Validated(value = CourseColumnParam.delete.class) @RequestBody CourseColumnParam courseColumnParam) {
        iCourseService.delColumn(courseColumnParam.getId());
        return AjaxResult.success();
    }


    /**
     * 切换专栏目录收费方式
     *
     * @author panweiliang
     * @param courseColumnParam 课程参数
     * @return AjaxResult
     */
    @Log(title = "专栏目录收费方式")
    @PostMapping("/column/feeType/change")
    public AjaxResult changeColumnFeeType(@Validated(value = CourseColumnParam.change.class)
                             @RequestBody CourseColumnParam courseColumnParam) {
        return AjaxResult.success(iCourseService.changeColumnFeeType(courseColumnParam.getId()));
    }
}
